package test;

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.Socket;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ThreadServerRead extends Thread {
	JTextArea textArea;
	JTextField textField;
	BufferedReader bufferedReader;
	Socket socket;
	ChatterWindows chatterWindows;

//	JFrame frame2 = new JFrame();
//	JLabel label = new JLabel("");
	MyThread myThread;
	CallWindow callWindow;

	public ThreadServerRead(BufferedReader bufferedReader, Socket socket, ChatterWindows chatterWindows,
			MyThread myThread) {
		super();
//		frame2.getContentPane().add(label);
		this.bufferedReader = bufferedReader;
		this.socket = socket;
		this.chatterWindows = chatterWindows;
		this.myThread = myThread;
		callWindow = new CallWindow(chatterWindows);
//		frame2.setSize(400, 40);
//		frame2.setVisible(true);
//		frame2.setLocation(0, -45);
	}

	@Override
	public void run() {
		while (true) {
			try {
				String data = bufferedReader.readLine();
//				System.out.println(data);
				if (data == null || data.equals("exitakilskdj")) {
					if (!socket.isClosed()) {
						socket.close();
					}
					JOptionPane.showMessageDialog(null, "Client đã tắt",
							"error", JOptionPane.ERROR_MESSAGE);
					chatterWindows.textAreaView.setBackground(Color.red);
					chatterWindows.setVisible(false);
					myThread.tray.remove(myThread.trayIcon);
					break;
				}else
				if (data.equals("focs875548915464")) {
					chatterWindows.buttonNewMess.setEnabled(false);
					chatterWindows.reading();
					chatterWindows.lblReadAlready.setText("Đã đọc");
				}else
				if (data.equals("lofo2484534498756")) {
					chatterWindows.buttonNewMess.setEnabled(true);
					chatterWindows.watting();
				}else
				if (data.equals("call75315987412369")) {
					callWindow.changeToCall();
					callWindow.setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2, (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2);
					callWindow.setCalled(CallWindow.CALLED);
					callWindow.setVisible(true);
				} else if (data.equals("new56474456488948")) {
//					System.out.println("new server");
					callWindow.changeToNewMess();
					callWindow.setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2, (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2);
					callWindow.setCalled(CallWindow.NEW_MESS);
					callWindow.setVisible(true);


				}else if (data.startsWith(ChatterWindows.TEMP_VIEW_STRING)) {
					data = data.replace(ChatterWindows.TEMP_VIEW_STRING, "");
					data = MaHoa.giaiMa(myThread.key, data);
					chatterWindows.textAreaTempView.setText(data);
					chatterWindows.textAreaTempView.setCaretPosition(chatterWindows.textAreaTempView.getDocument().getLength());
				} else {
					data = MaHoa.giaiMa(myThread.key, data);
					chatterWindows.textAreaView.setText(chatterWindows.textAreaView.getText() + "\n "+chatterWindows.name+"("+Chatter_Util.getCurrentTime()+"): " + data);
					chatterWindows.textAreaView.setCaretPosition(chatterWindows.textAreaView.getDocument().getLength());
					chatterWindows.textAreaTempView.setText("");
					if(myThread.displayNewMess == false || !chatterWindows.isFocused()){
						myThread.displayNewMess = true;
						myThread.trayIcon.displayMessage("Chatter", "you have a message", TrayIcon.MessageType.INFO);
						chatterWindows.newMess(true);

					}


//					if (!myThread.an) {
//						frame1.setVisible(true);
//						textArea.setSelectionEnd(textArea.getText().length());
//						textArea.setSelectionStart(textArea.getText().length());
//					}
//					label.setText(data);


//					if (!myThread.an) {
////						frame1.setVisible(true);
//						textField.setSelectionEnd(0);
//					}

//					int y = 40;
//					for (y = 40; y < 90; y += 2) {
//						try {
//							Thread.sleep(15);
//							frame2.setSize(400, y);
//						} catch (InterruptedException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//					}
//					try {
//						Thread.sleep(1000);
//					} catch (InterruptedException e1) {
//						// TODO Auto-generated catch block
//						e1.printStackTrace();
//					}
//					for (; y > 30; y -= 2) {
//						try {
//							Thread.sleep(15);
//							frame2.setSize(400, y);
//						} catch (InterruptedException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//
//					}
//					frame2.setVisible(false);
				}
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "Đã đóng kết nối", "error",
						JOptionPane.ERROR_MESSAGE);
				chatterWindows.textAreaView.setBackground(Color.red);
				myThread.tray.remove(myThread.trayIcon);
//				e.printStackTrace();
				break;

			}

		}
	}

}
